package com.aviation.sell_ticket.controller;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSONObject;
import com.aviation.sell_ticket.pojo.Aviation_user;
import com.aviation.sell_ticket.service.LoginService;
import com.aviation.sell_ticket.tool.ResultEnum;
import com.aviation.sell_ticket.tool.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("userAction")
public class LoginController {

    @Resource
    private LoginService loginService;

    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String name, String password, HttpServletRequest request) {
        if (Validator.isNull(name) || !Validator.isEmail(name)) {
            return ResultUtil.success(ResultEnum.MAILBOX_ERROR, null);
        } else if (!Validator.isNull(password)) {
            String regex = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=._])(?=\\S+$).{8,16}$";
            if (password.matches(regex)) {
                return loginService.login(name, password, request);
            }
        }
        return ResultUtil.success(ResultEnum.PASSWORD_ERROR, null);
    }

    @PostMapping("/sendVerificationCode")
    @ResponseBody
    public Map<String, Object> emailVerificationCode(String user_name) {
        if (!Validator.isNull(user_name) && Validator.isEmail(user_name)) {
            return loginService.emailVerificationCode(user_name);
        }
        return ResultUtil.success(ResultEnum.MAILBOX_ERROR, null);
    }

    @PostMapping("/logon")
    @ResponseBody
    public Map<String, Object> logon(Aviation_user aviationUser, String verificationCode) {
        //验证数据完整性
        if (Validator.isNull(aviationUser.getUser_name()) || !Validator.isEmail(aviationUser.getUser_name())) {
            return ResultUtil.success(ResultEnum.MAILBOX_ERROR, null);
        } else if (Validator.isNull(aviationUser.getPhone()) || !Validator.isMobile(String.valueOf(aviationUser.getPhone()))) {
            return ResultUtil.success(ResultEnum.INVALID_PHONE_NUMBER, null);
        } else if (Validator.isNull(verificationCode)) {
            return ResultUtil.success(ResultEnum.INVALID_VERIFICATION_CODE, null);
        } else if (!Validator.isNull(aviationUser.getUser_password())) {
            String user_password = aviationUser.getUser_password();
            String regex = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=._])(?=\\S+$).{8,16}$";
            if (user_password.matches(regex)) {
                return loginService.logon(aviationUser, verificationCode);
            }
        }
        return ResultUtil.success(ResultEnum.INVALID_PASSWORD, null);
    }

    @PostMapping("loginOut")
    @ResponseBody
    public Map<String,Object> loginOut(HttpServletRequest request){
        return loginService.loginOut(request.getHeader("Authorization"));
    }

    @PostMapping("changePassword")
    @ResponseBody
    public Map<String,Object> changePassword(Aviation_user aviationUser, String verificationCode){
        if (Validator.isNull(aviationUser.getUser_name()) || !Validator.isEmail(aviationUser.getUser_name())) {
            return ResultUtil.success(ResultEnum.MAILBOX_ERROR, null);
        }else if (Validator.isNull(verificationCode)) {
            return ResultUtil.success(ResultEnum.INVALID_VERIFICATION_CODE, null);
        } else if (!Validator.isNull(aviationUser.getUser_password())) {
            String user_password = aviationUser.getUser_password();
            String regex = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=._])(?=\\S+$).{8,16}$";
            if (user_password.matches(regex)) {
                return loginService.changePassword(aviationUser, verificationCode);
            }
        }
        return ResultUtil.success(ResultEnum.INVALID_PASSWORD, null);
    }

    @PostMapping("secondChangePassword")
    @ResponseBody
    public Map<String,Object> secondChangePassword(String oldPassword,String password,String redPassword,HttpServletRequest request){
        if(Validator.isNull(oldPassword) || Validator.isNull(password) || Validator.isNull(redPassword)){
            return ResultUtil.success(ResultEnum.DATA_IS_NULL, null);
        }
        String regex = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=._])(?=\\S+$).{8,16}$";
        if (!oldPassword.matches(regex) || !password.matches(regex) || !redPassword.matches(regex)) {
            return ResultUtil.success(ResultEnum.INVALID_PASSWORD, null);
        }
        if(!redPassword.equals(password)){
            return ResultUtil.success(ResultEnum.THE_TWO_PASSWORD_DO_NOT_MATCH, null);
        }
        return loginService.secondChangePassword(oldPassword,password,String.valueOf(request.getAttribute("userId")),request.getSession());
    }

}
